﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pay.Entity;
using pay.DAL.IDAL;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace pay.DAL
{
    public class OrderDAL:IOrderDAL
    {

        #region IOrderDAL 成员

        public Web_Order GetOrderByOrdernumber(OrderQuery query)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("sign", SqlDbType.Int, query.Sign);
            tool.AddSqlParameter("id", SqlDbType.Int, query.Id);
            tool.AddSqlParameter("ordernumber", SqlDbType.VarChar, 50, query.OrderNumber);
            tool.AddSqlParameter("username", SqlDbType.VarChar, 20, query.Username);
            tool.AddSqlParameter("where", SqlDbType.VarChar, 500, query.Where);
            SqlConnection con=new SqlConnection(DBConn.Con_emweb_new);
            con.Open();
            SqlDataReader reader= Emoney.DataAccess.SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "web_order_select_w", tool.GetSqlParameters());
            Web_Order m = null;
            try
            {
                if (reader.Read())
                {
                    m = new Web_Order();
                    m.Id = Tools.GetInt(reader["id"]);
                    m.Ordernumber = Tools.GetString(reader["ordernumber"]);
                    m.Allnum = Tools.GetInt(reader["allnum"]);
                    m.Allmoney = Tools.GetDouble(reader["allmoney"]);
                    m.Username = Tools.GetString(reader["username"]);
                    m.Status = Tools.GetInt(reader["status"]);
                    m.Orderdate = Tools.GetDateTime(reader["orderdate"]);
                    m.Delivertypeid = Tools.GetInt(reader["delivertypeid"]);
                    m.Paytypeid = Tools.GetInt(reader["paytypeid"]);
                    m.Remark = Tools.GetString(reader["remark"]);
                    m.Truename = Tools.GetString(reader["truename"]);
                    m.Gender = Tools.GetInt(reader["gender"]);
                    m.Tel = Tools.GetString(reader["tel"]);
                    m.Mobile = Tools.GetString(reader["mobile"]);
                    m.Province = Tools.GetString(reader["province"]);
                    m.City = Tools.GetString(reader["city"]);
                    m.Address = Tools.GetString(reader["address"]);
                    m.Postcode = Tools.GetString(reader["postcode"]);
                    m.Email = Tools.GetString(reader["email"]);
                    m.Invoice = Tools.GetString(reader["Invoice"]);
                    m.LinkMan = Tools.GetString(reader["linkMan"]);
                    m.ConsignTime = Tools.GetString(reader["consignTime"]);
                }
                return m;
            }
            catch (Exception ex)
            {

                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "GetOrderByOrdernumber", LogYuMin.LogStates.ERROR, ex);
          
            
            
            
            }
            finally
            {
                try
                {
                    reader.Close();
                    con.Close();
                }catch
                {
                }
            }
            return null;
        }


        public int UpdateOrderSatus(string orderNumber, int status)
        {
            try
            {   
                
                Tools tool = new Tools();
                tool.AddSqlParameter("@ordernumber", SqlDbType.VarChar, 50, orderNumber);
                tool.AddSqlParameter("@status", SqlDbType.Int, status);
                return Emoney.DataAccess.SqlHelper.ExecuteNonQuery(DBConn.Con_emweb_new, CommandType.StoredProcedure, "Web_order_update_status", tool.GetSqlParameters());
               
            }
            catch (Exception ex)
            {
               
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "UpdateOrderSatus", LogYuMin.LogStates.ERROR, ex);
          
            
            
                
                
                return 0;
            }
        }

        /// <summary>
        /// Web_Order对象的添加方法
        /// </summary>
        /// <param name="t">Web_Order实体对象</param>
        /// <returns>数据库影响行数</returns>
        public int Add(Web_Order t)
        {
            try
            {
                Tools tool = new Tools();
                tool.AddSqlParameter("id", SqlDbType.Int, 4, t.Id);
                tool.AddSqlParameter("ordernumber", SqlDbType.VarChar, 30, t.Ordernumber);
                tool.AddSqlParameter("allnum", SqlDbType.Int, 4, t.Allnum);
                tool.AddSqlParameter("allmoney", SqlDbType.Money, 8, t.Allmoney);
                tool.AddSqlParameter("username", SqlDbType.VarChar, 20, t.Username);
                tool.AddSqlParameter("status", SqlDbType.Int, 4, t.Status);
                tool.AddSqlParameter("orderdate", SqlDbType.DateTime, 8, t.Orderdate);
                tool.AddSqlParameter("delivertypeid", SqlDbType.Int, 4, t.Delivertypeid);
                tool.AddSqlParameter("paytypeid", SqlDbType.Int, 4, t.Paytypeid);
                tool.AddSqlParameter("remark", SqlDbType.VarChar, 5000, t.Remark);
                tool.AddSqlParameter("truename", SqlDbType.VarChar, 10, t.Truename);
                tool.AddSqlParameter("gender", SqlDbType.TinyInt, 1, t.Gender);
                tool.AddSqlParameter("tel", SqlDbType.VarChar, 20, t.Tel);
                tool.AddSqlParameter("mobile", SqlDbType.VarChar, 20, t.Mobile);
                tool.AddSqlParameter("province", SqlDbType.VarChar, 10, t.Province);
                tool.AddSqlParameter("city", SqlDbType.VarChar, 10, t.City);
                tool.AddSqlParameter("address", SqlDbType.VarChar, 100, t.Address);
                tool.AddSqlParameter("postcode", SqlDbType.Char, 6, t.Postcode);
                tool.AddSqlParameter("email", SqlDbType.VarChar, 50, t.Email);
                tool.AddSqlParameter("Invoice", SqlDbType.VarChar, 500, t.Invoice);
                tool.AddSqlParameter("linkMan", SqlDbType.VarChar, 30, t.LinkMan);
                tool.AddSqlParameter("consignTime", SqlDbType.VarChar, 1, t.ConsignTime);
                return Emoney.DataAccess.SqlHelper.ExecuteNonQuery(DBConn.Con_Etone, CommandType.StoredProcedure, "Web_Order_Add", tool.GetSqlParameters());
            }
            catch (Exception ex)
            {
               
                LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.Log).Write(GetType(), "Add", LogYuMin.LogStates.ERROR, ex);
          
            
                
                
                return 0;
            }
        }
        #endregion
    }
}
